package com.cloudcross.ssp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.Config;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.common.utils.SqlHelper;
import com.cloudcross.ssp.model.BannerOperator;
import com.cloudcross.ssp.model.Zone;
import com.cloudcross.ssp.service.IZoneService;
import com.cloudcross.ssp.web.widget.SelectorController.ISelector;
import com.cloudcross.ssp.web.widget.SelectorController.ISelectorProvider;

@Service
public class ZoneService implements IZoneService, ISelectorProvider {

    @Autowired
    private GenericIBatisDao myBatisDao;
    @Autowired
    Config config;
    // 文件系统可上传的文件后缀集合
    private static Set<String> noStuffixSet = new HashSet<String>();

    // 初始化可上传文件后缀
    static {
        noStuffixSet.add("jpg");
        noStuffixSet.add("gif");
    }

    @Override
    public boolean add(Zone t) {
        Date date = new Date();
        t.setUpdated(date);
        t.setSupdated(date.getTime() / 1000);
        myBatisDao.save("zoneSqlMapper.addZone", t);
        return true;
    }

    @Override
    public int countByParams(Map<String, Object> paramMap) {
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("searchValue", searchValue);
        paramMap.put("width", SqlHelper.doLike(paramMap.get("width")));
        paramMap.put("height", SqlHelper.doLike(paramMap.get("height")));
        paramMap.put("device", SqlHelper.doLike(paramMap.get("device")));
        paramMap.put("position", SqlHelper.doLike(paramMap.get("position")));
        paramMap.put("status", SqlHelper.doLike(paramMap.get("status")));
        return myBatisDao.get("zoneSqlMapper.countByParams", paramMap);
    }

    @Override
    public int countByParamsBack(Map<String, Object> paramMap) {
        return 0;
    }

    @Override
    public boolean edit(Zone zone) {
        Date date = new Date();
        zone.setUpdated(date);
        zone.setSupdated(date.getTime() / 1000);
        myBatisDao.save("zoneSqlMapper.editZone", zone);
        return true;
    }

    @Override
    public Zone findById(Long id) {
        return myBatisDao.get("zoneSqlMapper.selectByIdOperator", id);
    }

    @Override
    public Zone findByIdBack(Long id) {
        return myBatisDao.get("zoneSqlMapper.selectByIdBack", id);
    }

    @Override
    public List<Zone> findByParams(Map<String, Object> paramMap, Pager pager) {
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("searchValue", searchValue);
        paramMap.put("width", SqlHelper.doLike(paramMap.get("width")));
        paramMap.put("height", SqlHelper.doLike(paramMap.get("height")));
        paramMap.put("device", SqlHelper.doLike(paramMap.get("device")));
        paramMap.put("position", SqlHelper.doLike(paramMap.get("position")));
        paramMap.put("status", SqlHelper.doLike(paramMap.get("status")));
        if (pager == null) {
            return myBatisDao.getList("zoneSqlMapper.selectByParamsExp", paramMap);
        } else {
            paramMap.put("pager", pager);
            return myBatisDao.getList("zoneSqlMapper.selectByParams", paramMap);
        }

    }

    @Override
    public boolean updateStatus(List<Long> zoneIdList, int status) {
        myBatisDao.save("zoneSqlMapper.updateZoneStatus",
                MapBuilder.create(HashMap.class).add("zoneIdList", zoneIdList).add("updated", new Date()).add("status", status).map());
        return true;
    }

    // 导出excel
    @Override
    public List<Zone> findDeliverEffectAnalysis(Map<String, Object> paramMap) {
        return myBatisDao.getList("zoneSqlMapper.findDeliverEffectAnalysis", paramMap);
    }

    @Override
    public long findByName(String name) {
        return myBatisDao.get("zoneSqlMapper.findByName", name);
    }

    @Override
    public String deleteById(long id) {
        return myBatisDao.get("zoneSqlMapper.deleteById", id);
    }

    @Override
    public List<Zone> findByDeviceAndPlaceId(Map<String, Object> paramMap) {
        return myBatisDao.getList("zoneSqlMapper.findByDeviceAndPlaceId", paramMap);
    }

    @Override
    public Map<String, List<? extends ISelector>> provideData(String[] parentId) {
        Integer operatorId = null;
        if (null == parentId[0] || "".equals(parentId[0])) {
            operatorId = -1;
        } else {
            operatorId = Integer.parseInt(parentId[0]);
        }
        List<Zone> zoneList = null;
        zoneList = findByOperatorId(operatorId);
        return MapBuilder.create(HashMap.class).add("zoneList", zoneList).map();
    }

    @Override
    public List<Zone> findByOperatorId(Integer operatorId) {
        return myBatisDao.getList("zoneSqlMapper.findByOperatorIdOperator", operatorId);
    }

    @Override
    public List<Zone> findByOperatorIdBack(Integer operatorId) {
        return myBatisDao.getList("zoneSqlMapper.findByOperatorIdBack", operatorId);
    }

    @Override
    public Map<String, List<? extends ISelector>> provideDataBack(String[] parentId) {
        return null;
    }

    @Override
    public List<Zone> queryZoneByCondition(Map<String, Object> condition) {
        return myBatisDao.getList("zoneSqlMapper.queryZoneByCondition", condition);
    }

    @Override
    public List<Zone> queryZoneByAdgroupId(Long adgroupId) {
        return myBatisDao.getList("zoneSqlMapper.queryZoneByAdgroupId", adgroupId);
    }

    @Override
    public List<Zone> findByOperatorId(Long operatorId) {
        return null;
    }

    @Override
    public Integer queryZoneCount(Map<String, Object> condition) {
        return myBatisDao.get("zoneSqlMapper.queryZoneCount", condition);
    }

    @Override
    public List<Zone> queryZone(Map<String, Object> condition) {
        return myBatisDao.getList("zoneSqlMapper.queryZone", condition);
    }

    public List<Zone> queryZoneByOperatorIdStr(Map<String, Object> condition) {
        return myBatisDao.getList("zoneSqlMapper.queryZoneByOperatorIdStr", condition);
    }

    public String queryZoneNameById(Long id) {
        return myBatisDao.get("zoneSqlMapper.queryZoneNameById", id);
    }

    @Override
    public List<Zone> needCheck(Map<String, Object> paramMap) {
        return myBatisDao.getList("zoneSqlMapper.needCheck", paramMap);
    }

    @Override
    public List<Zone> operatorId(Map<String, Object> paramMap) {
        return myBatisDao.getList("zoneSqlMapper.operatorId", paramMap);
    }

    @Override
    public BannerOperator checkedStatus(Map<String, Object> paramMap) {
        return myBatisDao.get("bannerSqlMapper.checkedStatus", paramMap);
    }

    @Override
    public Integer checkedNum(Map<String, Object> paramMap) {
        return myBatisDao.get("bannerSqlMapper.checkedNum", paramMap);
    }

    @Override
    public Integer needCheckedAccountCount(Map<String, Object> paramMap) {
        return myBatisDao.get("zoneSqlMapper.needCheckedAccountCount", paramMap);
    }

    public Zone queryZoneByName(long name) {
        return this.myBatisDao.get("zoneSqlMapper.queryZoneByName", name);
    }

    // 广告位根据website_id匹配
    @Override
    public List<Zone> queryZoneWebsite(Map<String, Object> condition) {
        return myBatisDao.getList("zoneSqlMapper.queryZoneWebsite", condition);
    }
}
